package com.nitty.cash.loop.ui.presenter

import com.lib.common.permission.CommonPermissions
import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyApiUrl.addCommonParams
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.helper.NittyConfigsHelper
import com.nitty.cash.loop.utils.helper.NittyCreditStatusHelper
import com.nitty.cash.loop.utils.helper.NittyProcessHelper
import com.nitty.cash.loop.utils.helper.NittyProcessStatusHelper
import com.nitty.cash.loop.utils.helper.TestDataLibInvoke
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/9/10 13:41
 * @Annotation：未授信、未借款、已借款
 */
class WithdrawPresenter(private val iView: IViewContract.IWithdrawView): CommonPresenter(iView) {
    var amountInfo: JSONObject? = null

    /**
     * 获取授信状态
     */
    fun queryNittyCreditStatusAction(){
        // 进件信息未完成
        if (NittyProcessHelper.getAuthProcessList().size > 0){
            iView.showNittyStepIncompleteViews()
            return
        }

        val isCreditReviewing = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditReviewing()
        //授信中(未处理)
        if (isCreditReviewing){
            iView.showNittyStepIncompleteViews()
            return
        }

        val creditStatus = NittyProcessHelper.getAuthBeforeRiskStatus()
        val isCreditRejected = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditRejected(creditStatus)
        //已拒绝
        if (isCreditRejected){
            iView.showNittyStepIncompleteViews()
            return
        }

        // 授信通过 && 进件信息已完成 - 获取额度
        getNittyWithdrawAmountAction()
    }

    /**
     * 获取额度信息
     */
    private fun getNittyWithdrawAmountAction(){
        val httpUrl = NittyApiUrl.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_LOAN_LIMIT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    amountInfo = obj
                    iView.showNittyStepCompleteViews(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkNittyWithdrawPermissionsAgree(){
//        //是否是审核账号
//        val isConfigAccount = NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_IS_CONFIG_ACCOUNT)
//        if (isConfigAccount) {//审核账号，可以进行申贷操作
//            showNittyWithdrawPermissionSuccess()
//            return
//        }
        //订单被拒绝
        val applyStatus = NittyProcessHelper.getAuthApplyStatus()
        val orderStatus = NittyProcessHelper.getAuthOrderStatus()
        val isRejected = NittyProcessStatusHelper.StatusRepository(applyStatus,orderStatus).isRejected()
        if (isRejected){
            iView.showNittyOrderRejectedDialog()
            return
        }

        //是否进入过权限applist和sms权限页面
        val isShowPermission = NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_SHOW_PERMISSION_PAGE)
        val permissions = NittyConfigsHelper.getNittyPermissionsList()
        //权限全部同意 && 已经进入权限页面
        if (CommonPermissions.isGranted(iView.getAct(),permissions) && isShowPermission){
            showNittyWithdrawPermissionSuccess()
            return
        }
        iView.showNittyPermissionPage()
//        CommonPermissions.with(iView.getAct())
//            // 申请多个权限
//            .permission(permissions)
//            .request(object: OnPermissionCallback {
//                override fun onGranted(permissions: MutableList<String>, p1: Boolean) {
//                    if (p1){
//                        var permissionString = ""
//                        permissions.forEach {
//                            permissionString = "$permissionString$it,"
//                        }
//                        addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
//                        showNittyWithdrawPermissionSuccess()
//                    }
//                }
//                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
//                    var permissionString = ""
//                    permissions.forEach {
//                        permissionString = "$permissionString$it,"
//                    }
//                    addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
//
//                    CommonPermissions.startPermissionActivity(iView.getAct(),permissions)
//                }
//            })
    }

    /**
     * 权限检查成功
     */
    private fun showNittyWithdrawPermissionSuccess(){
        //上报AppList和SMS
        TestDataLibInvoke.sycReadSmsAppListSmsData()
        //进件信息未完成 - 进件信息页面
        if (NittyProcessHelper.getAuthProcessList().size > 0){
            iView.showNittyStepsPage()
            return
        }

        val creditStatus = NittyProcessHelper.getAuthBeforeRiskStatus()
        val isCreditRejected = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditRejected(creditStatus)
        //授信被拒绝 - 拒绝页面
        if (isCreditRejected){
            iView.showNittyCreditRejectedPage()
            return
        }

        val isCreditReviewing = NittyCreditStatusHelper.NittyCreditStatusRepository().isCreditReviewing()
        //进件信息已完成 && 授信中 - 授信状态查询页面
        if (isCreditReviewing){
            iView.showNittyCreditReviewPage()
            return
        }

        //进件信息已完成 && 授信已完成 - 申贷试算页面
        iView.showNittyApplyPage()
    }
}